<?php

/**
 * BKTiny Framework
 * Core_Registry class
 * 
 * LICENSE:
 * BKTiny Framework duoc xay dung va phat trien boi nhom BKloud - dai hoc bach khoa Ha Noi 
 * Ban duoc phep su dung framework nay cho muc dich hoc tap , nghien cuu va cac ung dung phi thuong mai
 * 
 * @copyright Copyright (c) 2011 BKloud - HPCC.
 * @version 0.1
 */
class Core_Registry implements ArrayAccess
{
    /**
     * Mang luu tru cac cap $key -> $val cua registry
     * @var array
     */
    private $_vars = array();

    /**
     * The instance of the registry
     * @var object of registry
     */
    private static $_instance;

    /**
     * Khong cho dung ham tao
     * @access private
     */
    private function __construct()
    {

    }

    /**
     * phuong thuc duy nhat de tao doi tuong registry
     * @access public
     * @return object of registry
     */
    public static function singleton()
    {

        if (!isset(self::$_instance))
        {

            $obj = __class__;
            self::$_instance = new $obj;
        }

        return self::$_instance;
    }

    /**
     * Khong cho sao chep doi tuong registry : Thong bao loi E_USER_ERROR neu sao chep doi tuong nay
     */
    public function __clone()
    {

        trigger_error('Cloning the registry is not permitted', E_USER_ERROR);
    }


    public function set($key, $var)
    {
        if (isset($this->_vars[$key]) == true)
        {
            throw new Exception('Unable to set var `' . $key . '`. Already set.');
        }

        $this->_vars[$key] = $var;
        return true;
    }

    public function get($key)
    {
        if (isset($this->_vars[$key]) == false)
        {
            return null;
        }

        return $this->_vars[$key];
    }

    public function remove($var)
    {
        unset($this->_vars[$key]);
    }

    public function offsetExists($offset)
    {
        return isset($this->_vars[$offset]);
    }

    public function offsetGet($offset)
    {
        return $this->get($offset);
    }

    public function offsetSet($offset, $value)
    {
        $this->set($offset, $value);
    }

    public function offsetUnset($offset)
    {
        unset($this->_vars[$offset]);
    }
}
